import { forwardRef, useImperativeHandle, useRef } from "react"

import { Ifocus } from "./index"

//useImperativeHandle 用于子组件暴露方法给Ref
type Iprops = {
    name: string
}
const Child = forwardRef<Ifocus, Iprops>((props, ref) => {
    //给input创建Ref 
    let ipt = useRef<HTMLInputElement | null>(null)
    useImperativeHandle(ref, () => {
        //返回的内容就是要暴露的内容
        return {
            getFocus() {
                // console.log(13)
                ipt.current!.focus()
                return true
            }
        }
    })

    return (
        <fieldset>
            <legend>子组件</legend>
            <input ref={ipt} type="text" />
        </fieldset>
    )
})

export default Child 